Validate component properties #264#340
Conversation
dash/dash.py
Outdated
| def add_context(*args, **kwargs): | ||
|
|
||
| output_value = func(*args, **kwargs) | ||
| setattr( |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
dash/dash.py
Outdated
| self._components[output.component_id]._validator.validate({ | ||
| output.component_property: output_value | ||
| }) | ||
| if output.component_property == 'children': |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
dash/dash.py
Outdated
| break | ||
| matched_state_value = matched_state.get('value', None) | ||
| args.append(matched_state_value) | ||
| setattr( |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
dash/development/base_component.py
Outdated
| pass | ||
|
|
||
|
|
||
| cerberus.Validator.types_mapping['component'] =\ |
There was a problem hiding this comment.
Let us validate Component objects with cerberus (useful for children prop)
dash/development/base_component.py
Outdated
| p not in ['dashEvents', 'fireEvent', 'setProps']] + ['**kwargs'] | ||
| ) | ||
|
|
||
| schema = {str(k): generate_property_schema(v) |
There was a problem hiding this comment.
Make cerberus schema for this dash component type.
|
Hi, I tried to I typed the command |
dash/development/base_component.py
Outdated
|
|
||
|
|
||
|
|
||
| # Forward declated Component class, see below. |
There was a problem hiding this comment.
did you mean 'declared'? PEP8 requires one space less :)
PS : this is my first review on dash code and I am mainly reading the code you wrote to learn more, my suggestion will mostly be about styling and spelling, I apologize in advance for that!
There was a problem hiding this comment.
my suggestion will mostly be about styling and spelling, I apologize in advance for that!
Note that we enforce PEP8 in our tests here:
Lines 38 to 39 in 4d93652
So, it's not necessary to review PEP8 issues in our PRs as they will get fixed before it gets merged into master.
|
To validate the components in the callbacks, I think we'll need to pass the component name back in the So, that is, we could modify the Dash API to include and then do a component lookup in the callback decorator |
|
^ Definitely, I was going to add this to the next iteration. You don't need this to validate against components in the initial layout, but I don't think there is any other way to validate against dynamically generated components without updating state in the backend after initialization. |
|
PR which adds 'type' and 'namespace' to the |
|
|
|
Oh ok, I would still integrate with the dev tools so just setting debug=True is required for it. |
|
Oh I thought you meant something different, yeah it should be disabled in production. |
|
@rmarren1 How would the component as prop validate ? proptype is |
|
@T4rk1n There is a custom type called dash/dash/development/base_component.py Lines 380 to 397 in 588ce3d |
|
Closing in favor of #452, which is the same PR but with a more readable commit history. Will link back to here for the relevant discussions. |
Allow tabs null children
PR for #264
Current Prerelease
Validation is on by default. To turn it off, you can set
app.config.disable_component_validation = TrueTest Cases to run for demo
Example Error Messages
PropTypes to Cerberus reference
array{'type': 'list'}bool{'type': 'boolean'}func{}object{'type': 'dict'}dictobject. We cannot be more general (e.g.collections.abc.Mapping) since the coreComponentis an instance of that.string{'type': 'string'}symbol{}node{'anyof': [{'type': 'component'}, {'type': 'boolean'}, {'type': 'number'}, {'type': 'string'}, {'type': 'list', 'schema': {'anyof': [{'type': 'component'}, {'type': 'boolean'}, {'type': 'number'}, {'type': 'string'}]}}]}instanceOf(Object){}oneOf(['val1', 2]){'allowed': [None, 'val1', 2]}'characters stripped off each end. This is becausemetadata.jsongeneration serializes the literal values as json, so for examplePropTypes.oneOf(['News', 'Photos'])serializes to["'News'", "'Photos'"]. reactjs/react-docgen#57oneOfType( [ PropTypes.string, PropTypes.bool ] ){'anyof': [{'type': 'string', 'type': 'boolean'}]}PropTypethat cannot be validated individually (e.g.PropType.func), no validation will occur and the schema will effectively be{}arrayOf( PropTypes.number ){'type': 'list', 'schema': {'type': 'number'}}objectOf( PropTypes.number ){'type': 'dict', 'valueschema': {'type': 'number'}}shape( { k1: PropTypes.string, k2: PropTypes.number } ){'type': 'dict', 'allow_unknown': False, 'schema': {'k1': {'type': 'string}, 'k2': {'type': 'number'}}}any{'type': ('boolean', 'number', 'string', 'dict', 'list')}